﻿<%@ WebHandler Language="VB" Class="ContainerStepWorkReportHandler" %>

Imports System
Imports System.Web
Imports BusinessRule.DTO
Imports System.Web.SessionState
Imports DatabaseAccess.DBUtility
Imports System.Data.OracleClient
Imports System.Data
Imports System.Linq
Imports System.Collections
Imports Newtonsoft.Json
Imports BusinessRule.Business
Imports BusinessRule.IBusiness
Imports BusinessRule.BusinessFactory
Imports BusinessRule.Common

'Description: 流水卡工序报工处理程序
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer: Zhangrj 
'create Date:20220629
'Rewriter:
'Rewrite Date:

Public Class ContainerStepWorkReportHandler : Implements IHttpHandler, IRequiresSessionState
    Dim m_UserInfo As New CurrentUserInfo
    Private m_Common As uLMSCommonIBusiness = uMESBusinessFactory.instance.GetuLMSCommonObject
    Private m_ReportBusiness As uLMSWorkReportIBusiness = uMESBusinessFactory.instance.GetuLMSWorkReportObject
    Private oIBusiness As uMESContainerInfoPrintIBusiness = uMESBusinessFactory.instance.GetContainerInfoPrintObject("LY_JX")
    Private m_DispatchObj As uLMSDispatchIBusiness = uMESBusinessFactory.instance.GetuLMSDispatchObject
    Private m_ContainerObj As uLMSContainerIBusiness = uMESBusinessFactory.instance.GetuLMSContainerObject
    Private strProductName As String
    Public htmlstr As String
    Public strHtmlTable As String
    Public strContainerName As String
    Public strMfgordername As String
    Private strContainerID As String
    Private isselfmfg As Boolean
    Private partCharacterType As String
    Private bomPersonMerge As String 'add:Wangjh 20180607 配套表人员是否合并
    Private startDate As String 'add:Wangjh 20180524
    Private strTechniqueEmp As String
    Private strPlanName As String
    Private wfFactoryName As String
    Private stepDispatchDt As DataTable
    Private periodicCheck As New PeriodicCheckBusiness
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim action As String = context.Request("action").ToString()
            m_UserInfo = context.Session("CurrentUserInfo")
            Select Case action
                Case "GetContainerStepReportList"
                    GetContainerStepReportList(context)
                Case "GetContainerDetailInfo"
                    GetContainerDetailInfo(context)
                Case "SaveStepWorkReport"
                    SaveStepWorkReport(context)
            End Select
        Catch ex As Exception
            Dim result = New uMESProcessResult()
            result.Result = False
            result.Message = ex.Message
            context.Response.ContentType = "text/html"
            context.Response.Write(JsonConvert.SerializeObject(result))
        End Try
    End Sub
    ''' <summary>
    ''' 获取待报工工序列表
    ''' </summary>
    ''' <param name="context"></param>
    ''' <remarks></remarks>
    Public Sub GetContainerStepReportList(ByVal context As HttpContext)
        Dim result As New uMESProcessResult()
        result.Result = True
        Dim para As New Dictionary(Of String, String)
        If m_UserInfo.FactoryID Is Nothing Then
            result.Result = False
            result.Message = "缓存过期"
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        para.Add("FactoryID", m_UserInfo.FactoryID)
        para.Add("WorkerID", m_UserInfo.EmployeeID)
        para.Add("PageSize", context.Request("pageSize").ToString())
        para.Add("PageIndex", context.Request("pageIndex").ToString())
        para.Add("ContainerName", context.Request("containerName").ToString())
        Dim scanStr As String = context.Request("scanContainer").ToString()
        If Not String.IsNullOrWhiteSpace(scanStr) Then
            para("ScanContainerName") = m_Common.ContainerScan(scanStr)
        End If
        result = m_ReportBusiness.GetNewStepWorkReportList(para)
        Dim Priority As Integer = 0
        Dim maxPriorityCode As Integer = 0
        If result.Result Then
            If result.DBTable.Rows.Count = 0 Then
                result.Message = "没有数据"
                result.Result = False
                context.Response.Write(JsonConvert.SerializeObject(result))
                Exit Sub
            End If
        End If
        context.Response.Write(JsonConvert.SerializeObject(result))
    End Sub
    ''' <summary>
    ''' 获取批次详细信息
    ''' </summary>
    ''' <param name="context"></param>
    ''' <remarks></remarks>
    Public Sub GetContainerDetailInfo(ByVal context As HttpContext)
        Dim result As New uMESProcessResult()
        result.Result = True
        Dim containerId As String = context.Request("containerId").ToString()
        Dim dic As New Dictionary(Of String, String)
        dic.Add("Containerid", containerId)

        Dim PR As uMESProcessResult = oIBusiness.GetContainerPrintInfo(dic)
        
        dic.Add("FactoryID", m_UserInfo.FactoryID)
        dic.Add("WorkerID", m_UserInfo.EmployeeID)
        stepDispatchDt = m_ReportBusiness.GetNewStepWorkReportByContainer(dic) '查询该工人的已派工序
        
        '工艺员查询
        strTechniqueEmp = PR.DBDataSet.Tables("dtMain").Rows(0)("TECHNIQUEEMPLOYNAME").ToString
        strProductName = PR.DBDataSet.Tables("dtMain").Rows(0)("productname").ToString()
        strPlanName = PR.DBDataSet.Tables("dtMain").Rows(0)("PlanFullName").ToString
        wfFactoryName = PR.DBDataSet.Tables("dtMain").Rows(0)("wffactoryname").ToString() '工艺分厂
        
        result.ReturnData = IniPrintInfo(PR, False)
        context.Response.Write(JsonConvert.SerializeObject(result))
    End Sub
    ''' <summary>
    ''' 填充打印信息
    ''' </summary>
    ''' <param name="PR"></param>
    ''' <remarks></remarks>
    Private Function IniPrintInfo(ByVal PR As uMESProcessResult, ByVal IsSubWorkflow As Boolean) As String
        Dim Originalqty = PR.DBDataSet.Tables("dtMain").Rows(0)("Originalqty").ToString()
        Dim conType As Integer = Val(PR.DBDataSet.Tables("dtMain").Rows(0)("CONTAINERTYPE").ToString())
        strMfgordername = PR.DBDataSet.Tables("dtMain").Rows(0)("Mfgordername").ToString()
        isselfmfg = If(PR.DBDataSet.Tables("dtMain").Rows(0)("isselfmfg").ToString() = "0", True, False)
        startDate = PR.DBDataSet.Tables("dtMain").Rows(0)("originalstartdate").ToString()
        Dim gys As String = GetDataValue(PR.DBDataSet.Tables("dtDataPoint"), "供应商", False)
        '产品特性 add:Wangjh 20150517
        partCharacterType = PR.DBDataSet.Tables("dtMain").Rows(0)("charactertype").ToString()
        If partCharacterType <> "关键件" And partCharacterType <> "重要件" Then
            partCharacterType = ""
        End If
        '
        Dim strHtmlTitle As String = SetTableTitle(conType, partCharacterType, startDate, gys)
        Dim strHtmlTableMain As String = SetTableMain(PR, Originalqty)
        Dim strHtmlTableDetails As String = SetTableDetail(PR, Originalqty)
        Return String.Format("{0}{1}{2}", strHtmlTitle, strHtmlTableMain, strHtmlTableDetails)
    End Function
    ''' <summary>
    ''' 生成表头
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetTableTitle(ByVal type As Integer, ByVal partCharacterType As String, ByVal originalstartdate As String, ByVal gys As String) As String

        Dim parttype As String = If(isselfmfg, "零件", "组件")
        If Not String.IsNullOrWhiteSpace(originalstartdate) Then
            originalstartdate = DateTime.Parse(originalstartdate).ToString("yyyy年MM月dd日")
        End If
        Dim sb As New StringBuilder()
        sb.Append("<table class=table1 >")
        sb.Append("<tr><th colspan=3 valign='bottom' style='height:60px;text-align: center;position:relative'>")
        If parttype = "零件" Then
            sb.AppendLine("<div style='width:100%;height:36px;text-align:left;position:absolute;left:0;top:0px' ><div style='float:left'>供应商:</div><span style='text-align:left;font-weight:normal;'>" + gys + "</span></div>")
        End If
        If type = 7 Then
            sb.AppendFormat("<span style=font-size:32px;>单件加工{0}工艺流水卡片</span><div style='float:right;margin-top:10px'>" + partCharacterType + "</div></th></tr>", parttype)
        Else
            sb.AppendFormat("<span style=font-size:32px;>批量加工{0}工艺流水卡片</span><div style='float:right;margin-top:10px'>" + partCharacterType + "</div></th></tr>", parttype)
        End If

        sb.AppendFormat("<tr><td style='text-align: left;padding-left:10px;'>{0}</td>", wfFactoryName) 'update:Wangjh 2017-7-6 将工厂更改为当前用户所属的 20180927更改为工艺分厂
        sb.AppendFormat("<td style='text-align: center'> {0} </td>", originalstartdate)
        sb.Append("</tr></table>")
        SetTableTitle = sb.ToString
    End Function

    ''' <summary>
    ''' 生成流水卡信息
    ''' </summary>
    ''' <param name="PR"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetTableMain(ByVal PR As uMESProcessResult, ByVal Originalqty As Integer) As String
        Dim dtMain As DataTable = PR.DBDataSet.Tables("dtMain")
        Dim sbHtml As New StringBuilder
        Dim productName = dtMain.Rows(0)("Productname").ToString()
        Dim ProductDesc = dtMain.Rows(0)("ProductDesc").ToString()
        Dim workflowRoute As String = dtMain.Rows(0)("workflowroute").ToString() '分段件号
        Dim familyName = dtMain.Rows(0)("familylistnames")
        Dim partRev As String = dtMain.Rows(0)("Part_RevID").ToString '设计图版次 
        Dim prosseRev As String = dtMain.Rows(0)("ly2sequence").ToString '工艺规程版次 

        'Modify by Yuxj20200528 型号包含WP,全通，但是不包含WP，设计图版次和工艺版次打印斜杠
        If (familyName.ToString.Contains("WP") Or familyName.ToString.Contains("全通")) And (Not familyName.ToString.Contains("WS")) And (Not familyName.ToString.Contains("KWP2")) Then
            partRev = "/"
            prosseRev = "/"
        End If

        Dim workflowType As String = dtMain.Rows(0)("WorkflowType").ToString '工艺类型
        Dim prosseNo As String = dtMain.Rows(0)("process_no").ToString '原始工艺号

        If workflowType.Trim <> "临时" Then
            prosseNo = ""
        End If
        Dim wfFactoryID = dtMain.Rows(0)("wffactoryid").ToString()
        wfFactoryName = dtMain.Rows(0)("wffactoryname").ToString()
        bomPersonMerge = m_Common.GetFatoryAttributes(wfFactoryID, "配套表人员合并")
        Dim Containername = dtMain.Rows(0)("Containername").ToString()
        strContainerName = Containername
        sbHtml.Append("<table class='table2' ><tr ><td width='110' >设计图版次</td><td width='60'>" + partRev + "</td><td width='110'>工艺规程版次</td><td width='60'>" + prosseRev + "</td><td width='120'>临时工艺规程编号</td><td>" + prosseNo + "</td></tr></table>")
        sbHtml.Append("<table class='table2'>")
        Dim dtDataPoint As DataTable = PR.DBDataSet.Tables("dtDataPoint")
        If Not String.IsNullOrWhiteSpace(workflowRoute) Then
            strProductName = workflowRoute
        End If
        If wfFactoryName.Contains("机匣") And dtDataPoint.Select("dataname='ChildWFContianer'").Length > 0 Then
            strProductName += "(H)"
        End If

        If isselfmfg Then '零件
            sbHtml.AppendFormat("<tr><td colspan=2>生产订单号</td><td colspan=2>{0}</td><td colspan=2>批次号</td><td colspan=2>{1}</td></tr>", strMfgordername, Containername.Replace(productName + "/", ""))
            Dim Originalstartdate = dtMain.Rows(0)("Originalstartdate").ToString()
            Dim Materialtrademark = dtMain.Rows(0)("PlanMaterialtrademark").ToString '材料牌号
            Dim MaterialSpecification = dtMain.Rows(0)("PlanMaterialSpecification").ToString '材料规格
            Dim materialSize = dtMain.Rows(0)("materialSize").ToString '毛料规格
            Dim strMaterialtEmp As String = "" '库房保管
            Dim strmaterialCheckEmp As String = "" '器材检验

            Dim strFilter As String = "dataname='实发数/顺序号'"
            Dim tmpRows As DataRow() = dtDataPoint.Select(strFilter)
            Dim shifa As String = ""
            If tmpRows.Length > 0 Then
                If tmpRows(0).IsNull("datavalue") = False Then
                    shifa = tmpRows(0)("datavalue").ToString()
                End If
                If tmpRows(0).IsNull("splitfromid") = False Then '如果该批次是由分批而来的实发数就是分批数 add:Wangjh 2017-7-25
                    shifa = tmpRows(0)("originalqty").ToString()
                End If
            End If

            Dim rongluChild As String = GetDataValue(dtDataPoint, "熔炉号子")
            Dim rongluP As String = GetDataValue(dtDataPoint, "熔炉号母")
            Dim jianyan As String = GetDataValue(dtDataPoint, "检验编号")
            Dim paihao As String = GetDataValue(dtDataPoint, "代料牌号", True)
            Dim paihaoge As String = GetDataValue(dtDataPoint, "代料规格", True)
            Dim danhao As String = GetDataValue(dtDataPoint, "代料单号", True)
            Dim pingxing As String = GetDataValue(dtDataPoint, "平行作业工序", True)
            Dim pingxingBill As String = GetDataValue(dtDataPoint, "平行作业单号", True)

            strMaterialtEmp = GetDataValue(dtDataPoint, "库房保管")
            strmaterialCheckEmp = GetDataValue(dtDataPoint, "器材检验")
            sbHtml.Append("<tr><td width='90px'>零件号</td>")

            sbHtml.AppendFormat("<td width='110' id='pName'>{0}</td>", strProductName)

            sbHtml.AppendFormat("<td width='75'>名称</td><td width='120'>{0}</td>", ProductDesc)
            sbHtml.AppendFormat("<td width='55'>型别</td><td width='120' style='font-size: 12px'>{0}</td>", familyName)
            sbHtml.AppendFormat("<td width='65'>计划员</td><td width='100'>{0}</td></tr>", strPlanName)

            sbHtml.Append("<tr><td>熔炉号</td>")
            If String.IsNullOrWhiteSpace(rongluChild) And String.IsNullOrWhiteSpace(rongluP) Then
                sbHtml.Append("<td></td>")
            ElseIf String.IsNullOrWhiteSpace(rongluChild) Then
                sbHtml.AppendFormat("<td>{0}</td>", rongluP)
            ElseIf String.IsNullOrWhiteSpace(rongluP) Then
                sbHtml.AppendFormat("<td>{0}</td>", rongluChild)
            Else
                sbHtml.AppendFormat("<td style='font-size :small'>子：{0}</p>母：{1}</td>", rongluChild, rongluP)
            End If

            sbHtml.AppendFormat("<td>计划数</td><td>{0}</td>", Originalqty)
            sbHtml.AppendFormat("<td>实发数/顺序号</td><td>{0}</td>", shifa)
            sbHtml.AppendFormat("<td>工艺员</td><td>{0}</td></tr>", strTechniqueEmp)

            sbHtml.AppendFormat("<tr><td>材料牌号</td><td style='font-size: 12px'>{0}</td>", Materialtrademark)

            sbHtml.AppendFormat("<td>检&nbsp;验编&nbsp;号</td><td>{0}</td>", jianyan)
            sbHtml.AppendFormat("<td>库&nbsp;房保&nbsp;管</td><td>{0}</td>", strMaterialtEmp)
            'sbHtml.Append("<td>器&nbsp;材检&nbsp;验</td>")
            'If String.IsNullOrWhiteSpace(pingxingBill) Then
            '    sbHtml.AppendFormat("<td>{0}</td>", strmaterialCheckEmp)
            'Else
            '    sbHtml.Append("<td></td>")
            'End If
            sbHtml.AppendFormat("<td>器&nbsp;材检&nbsp;验</td><td>{0}</td>", strmaterialCheckEmp)
            sbHtml.Append("</tr>")

            sbHtml.AppendFormat("<td>材料规格</td><td style='font-size: 12px'>{0}</td>", MaterialSpecification)
            sbHtml.AppendFormat("<td>毛&nbsp;料规&nbsp;格</td><td style='font-size: 12px'>{0}</td>", materialSize)
            sbHtml.AppendFormat("<td>代&nbsp;料牌&nbsp;号</td><td>{0}</td>", paihao)
            sbHtml.AppendFormat("<td>代&nbsp;料规&nbsp;格</td><td>{0}</td></tr>", paihaoge)

            sbHtml.AppendFormat("<tr><td>代料单号</td><td>{0}</td>", danhao)
            'Modify by yuxj20200528 平行作业单->例外放行审批单号 平行作业工序->例外放行工序
            sbHtml.AppendFormat("<td>例外放行审批单号</td><td>{0}</td>", pingxingBill)
            sbHtml.AppendFormat("<td>例外放行工序</td><td>{0}</td>", pingxing) '
            sbHtml.Append("<td>分&nbsp;厂检&nbsp;验</td>")
            If dtDataPoint.Rows(0).IsNull("splitfromid") = False Then '如果该批次是由分批而来的 add:Wangjh 2017-7-25
                sbHtml.AppendFormat("<td>{0}</td>", dtDataPoint.Rows(0)("checkemployeeName").ToString())
            Else
                'sbHtml.Append("<td></td>")
                'add zhunan 2021.1.18 增加显示【分厂检验】（取第一道工序实际的检验员）
                Dim fcjy As String = ""
                If PR.DBDataSet.Tables("dtContainerStep").Columns.Contains("checkemployeename") Then
                    Dim temp = PR.DBDataSet.Tables("dtContainerStep").Select("factoryid='" + wfFactoryID + "'")
                    If temp.Length > 0 Then
                        fcjy = temp(0)("checkemployeename").ToString
                    End If
                End If
                sbHtml.AppendFormat("<td>{0}</td>", fcjy)
            End If
            sbHtml.Append("</tr>")

            'add zhunan 2021.1.18 打印顾客代表、热表批次号
            Dim gkdb As String = GetDataValue(dtDataPoint, "顾客代表")
            sbHtml.Append("<tr>")
            Dim dtRbContainerList As DataTable = PR.DBDataSet.Tables("dtRbContainerList") '热表批次号
            If dtRbContainerList.Rows.Count = 0 Then
                sbHtml.Append("<td colspan=1>热表批次</td> <td colspan=2></td> <td colspan=2></td> <td colspan=1></td>")
            ElseIf dtRbContainerList.Rows.Count = 1 Then
                sbHtml.AppendFormat("<td colspan=1>热表批次</td> <td colspan=2>{0}</td><td colspan=2></td><td colspan=1></td>", dtRbContainerList.Rows(0)("rbtogetherflag").ToString())
            ElseIf dtRbContainerList.Rows.Count = 2 Then
                sbHtml.AppendFormat("<td colspan=1>热表批次</td> <td colspan=2>{0}</td><td colspan=2>{1}</td><td colspan=1></td>",
                                    dtRbContainerList.Rows(0)("rbtogetherflag").ToString(),
                                    dtRbContainerList.Rows(1)("rbtogetherflag").ToString())
            Else
                sbHtml.AppendFormat("<td colspan=1>热表批次</td> <td colspan=2>{0}</td><td colspan=2>{1}</td><td colspan=1>{2}</td>",
                                    dtRbContainerList.Rows(0)("rbtogetherflag").ToString(),
                                    dtRbContainerList.Rows(1)("rbtogetherflag").ToString(),
                                    dtRbContainerList.Rows(2)("rbtogetherflag").ToString())
            End If

            sbHtml.AppendFormat("<td colspan=1>顾&nbsp;客代&nbsp;表</td><td colspan=1>{0}</td>", gkdb)
            sbHtml.Append("</tr>")

            '追加不合格审理单号 add by zhangrj 2022-2-23
            Dim unQualifiedNum As String = GetDataValue(dtDataPoint, "不合格审理单号", True)
            sbHtml.AppendFormat("<tr><td colspan=2>不合格审理单号</td><td colspan=6>{0}</td></tr>", unQualifiedNum)
        Else '组件
            sbHtml.AppendFormat("<tr><td colspan=4>生产订单号</td><td colspan=4>{0}</td></tr>", strMfgordername)
            sbHtml.Append("<tr><td width='60'>组合件号</td>")

            sbHtml.AppendFormat("<td width='120' id='pName'>{0}</td>", strProductName)

            sbHtml.AppendFormat("<td width='55'>名称</td><td width='120'>{0}</td>", ProductDesc)
            sbHtml.AppendFormat("<td width='55'>型别</td><td colspan=3 style ='font-size :10px'>{0}</td></tr>", familyName)
            sbHtml.AppendFormat("<tr><td>批次号</td><td>{0}</td>", Containername.Replace(productName + "/", ""))
            sbHtml.AppendFormat("<td>数量</td><td>{0}</td>", Originalqty)
            sbHtml.AppendFormat("<td>计划员</td><td width='55'>{0}</td>", strPlanName)
            sbHtml.AppendFormat("<td width='55'>工艺员</td><td width='55'>{0}</td></tr>", strTechniqueEmp)
            ' sbHtml.AppendLine("<tr><td colspan=1>热表批次</td><td colspan=1></td><td colspan=2></td><td colspan=2></td><td colspan=1>顾&nbsp;客代&nbsp;表</td><td colspan=1></td></tr>")
            'sbHtml.AppendLine("<tr><td colspan=2>热表批次</td><td colspan=2></td><td colspan=2></td><td colspan=2></td></tr>")
            Dim dtRbContainerList As DataTable = PR.DBDataSet.Tables("dtRbContainerList") '热表批次号
            If dtRbContainerList.Rows.Count = 0 Then
                sbHtml.Append("<td colspan=2>热表批次</td> <td colspan=2></td> <td colspan=2></td> <td colspan=2></td>")
            ElseIf dtRbContainerList.Rows.Count = 1 Then
                sbHtml.AppendFormat("<td colspan=2>热表批次</td> <td colspan=2>{0}</td><td colspan=2></td><td colspan=2></td>", dtRbContainerList.Rows(0)("rbtogetherflag").ToString())
            ElseIf dtRbContainerList.Rows.Count = 2 Then
                sbHtml.AppendFormat("<td colspan=2>热表批次</td> <td colspan=2>{0}</td><td colspan=2>{1}</td><td colspan=2></td>",
                                    dtRbContainerList.Rows(0)("rbtogetherflag").ToString(),
                                    dtRbContainerList.Rows(1)("rbtogetherflag").ToString())
            Else
                sbHtml.AppendFormat("<td colspan=2>热表批次</td> <td colspan=2>{0}</td><td colspan=2>{1}</td><td colspan=2>{2}</td>",
                                    dtRbContainerList.Rows(0)("rbtogetherflag").ToString(),
                                    dtRbContainerList.Rows(1)("rbtogetherflag").ToString(),
                                    dtRbContainerList.Rows(2)("rbtogetherflag").ToString())
            End If
        End If

        sbHtml.Append("</table>")


        SetTableMain = sbHtml.ToString()
    End Function
    
    ''' <summary>
    ''' 生成流水卡工序
    ''' </summary> 
    ''' <param name="PR"></param>
    ''' <param name="qty"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetTableDetail(ByVal PR As uMESProcessResult, ByVal qty As Integer) As String
        SetTableDetail = String.Empty
        Try
            Dim dtMain As DataTable = PR.DBDataSet.Tables("dtMain")
            Dim dtContainerStep As DataTable = PR.DBDataSet.Tables("dtContainerStep")
            'Dim dtCheckItemList As DataTable = PR.DBDataSet.Tables("dtCheckItemList")
            Dim conType As Integer = Val(PR.DBDataSet.Tables("dtMain").Rows(0)("CONTAINERTYPE").ToString())
            Dim wipQty = dtMain.Rows(0)("qty").ToString

            Dim strProdcut As String = dtMain.Rows(0)("productname").ToString().Trim()
            Dim strMemo As String = ""
            '得到关闭备注信息
            Dim strCloseContainer As String = ""
            If dtMain.Rows(0).IsNull("CloseContianer") = False Then
                strCloseContainer = dtMain.Rows(0)("CloseContianer").ToString().Trim()
                strMemo = oIBusiness.GetColseMemo(strCloseContainer)
            End If
            '得到开始工序
            '得到关闭备注信息
            Dim strBeginStepName As String = ""
            If dtMain.Rows(0).IsNull("BeginStep") = False Then
                strBeginStepName = dtMain.Rows(0)("BeginStep").ToString().Trim()
            End If
            Dim sbHtml As New StringBuilder
         
            Dim specname = String.Empty
            Dim specid = String.Empty
            Dim stepid = String.Empty
            Dim sequenceNO = String.Empty
            Dim stepName As String = ""

            sbHtml.Append(SetTableSpecHeader(True))

            For i As Integer = 0 To dtContainerStep.Rows.Count - 1
                Dim sbSpecHtml As New StringBuilder
                Dim strBmemo As String = ""
                specname = String.Empty
                specid = String.Empty
                sequenceNO = String.Empty
                stepName = String.Empty 'add:Wangjh 2017-8-4
                stepid = String.Empty
                SetSpecNameInfo(specname, stepid, specid, stepName, sequenceNO, dtContainerStep.Rows(i))
                sbSpecHtml.Append(SetSpecInfo(specname, sequenceNO, stepid, wipQty, strBmemo, dtContainerStep, i))
                sbHtml.Append(sbSpecHtml.ToString)
            Next
            sbHtml.Append("</table>")
            SetTableDetail = sbHtml.ToString()
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    
    ''' <summary>
    ''' 生产工序表头
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetTableSpecHeader(ByVal isfirstpage As Boolean) As String
        Dim sbTable3Header As New StringBuilder
        If isfirstpage Then
            sbTable3Header.Append("<table id=originaltable class=table3 style='BORDER-TOP: medium none;'>")
        Else
            sbTable3Header.Append("<table id=originaltable class=table3 style='BORDER-TOP: medium none;PAGE-BREAK-AFTER: always'>")
        End If
        sbTable3Header.Append("<tr><td rowspan=2 width='65'><div class=t_1>工序</div></td>")
        sbTable3Header.Append("<td rowspan=2 width='150'>名称</td>")
        sbTable3Header.Append("<td rowspan=2 width='35'>数量</td>")
        sbTable3Header.Append("<td colspan=4>自检结果（件数）</td>")

        sbTable3Header.Append("<td rowspan=2 width='80'>加&nbsp;&nbsp;工日&nbsp;&nbsp;期</td>")
        sbTable3Header.Append("<td  rowspan=2 width='35' style='BORDER-RIGHT: medium none'>工</td>")
        sbTable3Header.Append("<td  rowspan=2 width='45' style='BORDER-LEFT: medium none'>人</td>")
        sbTable3Header.Append("<td rowspan=2 width='55'>检验印</td>")
        sbTable3Header.Append("<td rowspan=2 width='105'>备注</td>")
        sbTable3Header.Append("<tr><td width='30'><div class=t_2>合格</div></td>")
        sbTable3Header.Append("<td width='30'><div class=t_2>报废</div></td>")
        sbTable3Header.Append("<td width='30'><div class=t_2>超差</div></td>")
        sbTable3Header.Append("<td width='30'><div class=t_2>返修</div></td></tr>")
        SetTableSpecHeader = sbTable3Header.ToString
    End Function
    
    ''' <summary>
    ''' 工序基本信息处理
    ''' </summary>
    ''' <param name="specname"></param>
    ''' <param name="specid"></param>
    ''' <param name="stepName"></param>
    ''' <param name="sequenceNO"></param>
    ''' <param name="dr"></param>
    ''' <remarks></remarks>
    Private Sub SetSpecNameInfo(ByRef specname As String, ByRef stepid As String, ByRef specid As String, ByRef stepName As String, ByRef sequenceNO As String, ByVal dr As DataRow)

        specname = dr("specname").ToString()
        specid = dr("specid").ToString()
        stepName = dr("workflowstepname").ToString()
        stepid = dr("workflowstepid").ToString
        If String.IsNullOrWhiteSpace(specname) Then 'add:Wangjh 2017-8-3 
            specname = stepName
        End If

        Dim arrTemp = stepName.Split("-")
        sequenceNO = arrTemp(0)
        specname = stepName.Replace(sequenceNO + "-", "")

        If Not String.IsNullOrWhiteSpace(dr("opdesc").ToString()) Then
            specname = dr("opdesc").ToString()
        End If
        '

        'add:Wangjh 增加工序号和工序名赋值 20171206
        If Not String.IsNullOrWhiteSpace(dr("specno").ToString()) Then
            sequenceNO = dr("specno").ToString()
        End If
        '处理工序号位数
        Try
            If IsNumeric(sequenceNO) Then
                sequenceNO = Integer.Parse(sequenceNO).ToString
            End If
        Catch ex As Exception

        End Try

        '关键工序，在工序号前加G
        If dr("iskey").ToString() = "1" Then
            sequenceNO = "G" + sequenceNO
        End If
    End Sub
    
    ''' <summary>
    ''' 生成工序表格
    ''' </summary>
    ''' <param name="specname">工序名</param>
    ''' <param name="sequenceNO">工序号</param>
    ''' <param name="stepid">工序ID</param>
    ''' <param name="wipQty">在制数</param>
    ''' <param name="strBmemo">备注</param>
    ''' <param name="dtContainerStep">工序列表</param>
    ''' <param name="i">行数</param>    
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetSpecInfo(ByVal specname As String, ByVal sequenceNO As String, ByVal stepid As String, ByVal wipQty As String, ByVal strBmemo As String, ByVal dtContainerStep As DataTable, ByVal i As Integer) As String
        Dim sbSpecHtml As New StringBuilder
        If Not dtContainerStep.Columns.Contains("reportdate") Then
            dtContainerStep.Columns.Add("reportdate")
        End If
        '报工
        Dim drsReport As DataRow() = dtContainerStep.Select(" workflowstepid='" + stepid + "' and reportdate is not null")
        ' Dim dicField = GetDetailsField(drsReport, drsQuality, drsSpecLastCheck, drsConventionCheck)
        sbSpecHtml.AppendFormat("<tr idtype=spec  ><td rowspan=1>{0}</td><td rowspan=1 class=t_4", sequenceNO)

        If specname.Length > 15 Then
            sbSpecHtml.Append(" style='font-size: 10px'>")
        Else
            sbSpecHtml.Append(">")
        End If
        sbSpecHtml.AppendFormat("{0}</td>", specname)
        '添加检验备注 add zhangrj 20220608
        If stepid <> "" Then
            Dim drsCheckNotes As DataRow() = dtContainerStep.Select("workflowstepid='" + stepid + "'")
            strBmemo += drsCheckNotes(0)("NOTES").ToString + ","

            If dtContainerStep.Columns.Contains("ISCOMMISSION") Then
                If drsCheckNotes(0)("CONTROLCODE").ToString() = "LY05" Or drsCheckNotes(0)("ISCOMMISSION").ToString() = "1" Then
                    strBmemo += "外委加工,"
                End If
            End If
            For Each cknote As DataRow In drsCheckNotes
                If Not String.IsNullOrWhiteSpace(cknote("CHECKNOTE").ToString) Then
                    strBmemo += cknote("CHECKNOTE").ToString + ","
                End If
                If Not String.IsNullOrWhiteSpace(cknote("WORKNOTE").ToString) Then
                    strBmemo += cknote("WORKNOTE").ToString + ","
                End If
            Next
            strBmemo = strBmemo.Trim(",")
        End If
        '如果没有报工数据
        If drsReport.Length = 0 Then
            Dim drs As DataRow() = stepDispatchDt.Select("workflowstepid='" + stepid + "'")
            '工序存在派工，添加报工按钮
            If drs.Length > 0 Then
                strBmemo = "<input class='easyui-textbox' style='width: 150px; height: 22px' />" '备注输入框
                '报工按钮
                Dim reportBtn As String = "<a class='easyui-linkbutton' style='width:80px' data-stepid='" + stepid + "' data-disid='" + drs(0)("containerstepdispatchid").ToString() + "'>报工</a>"
                sbSpecHtml.Append("<td></td><td></td><td></td><td></td><td></td><td></td><td colspan=2>" + reportBtn + "</td><td></td><td style='text-align: left;font-size: 10px'>" + strBmemo + "</td>")
            Else
                sbSpecHtml.Append("<td></td><td></td><td></td><td></td><td></td><td></td><td colspan=2></td><td></td><td style='text-align: left;font-size: 10px'>" + strBmemo + "</td>")
            End If
        Else
            Dim strTmpMemo As String = ""
            If dtContainerStep.Rows(i)("checktype").ToString = "3" Then '试验件报工
                strTmpMemo = "试件报工:" & dtContainerStep.Rows(i)("ReportQty").ToString + ","
                dtContainerStep.Rows(i)("ReportQty") = wipQty
                dtContainerStep.Rows(i)("passqty") = wipQty
                dtContainerStep.Rows(i)("lossqty") = DBNull.Value
            End If
            Dim reportDate As String = ""
            If Not dtContainerStep(i).IsNull("reportdate") Then
                reportDate = DateTime.Parse(dtContainerStep(i)("reportdate")).ToString("yyyy/MM/dd")
            End If

            sbSpecHtml.Append("<td>" + dtContainerStep.Rows(i)("ReportQty").ToString + "</td><td style='")
            sbSpecHtml.Append("'>" + dtContainerStep.Rows(i)("passqty").ToString + "</td><td>" + dtContainerStep.Rows(i)("lossqty").ToString + "</td>")
            sbSpecHtml.Append("<td>" + dtContainerStep.Rows(i)("defectqty").ToString + "</td><td>" + dtContainerStep.Rows(i)("ReworkQty").ToString + "</td><td>" + reportDate + "</td>")

            sbSpecHtml.Append("<td colspan=2 style='")
            If dtContainerStep.Rows(i)("ReportEmployeeName").ToString.Length > 6 Then
                sbSpecHtml.Append(" font-size: 10px")
            End If

            '检验工序 报工人显示横岗 add zhunan 2022.3.15
            If dtContainerStep.Rows(i)("LASTCHECK").ToString = "1" Or dtContainerStep.Rows(i)("CONTROLCODE").ToString = "LY05" Then
                sbSpecHtml.Append("'>" + "--" + "</td><td style='")
            Else
                sbSpecHtml.Append("'>" + dtContainerStep.Rows(i)("ReportEmployeeName").ToString + "</td><td style='")
            End If
            
            If dtContainerStep.Rows(i)("CONTROLCODE").ToString = "LY05" Then
                sbSpecHtml.Append("'>" + "--" + "</td><td style='text-align: left;")
            Else
                If dtContainerStep.Rows(i)("checkemployeename").ToString.Length > 6 Then
                    sbSpecHtml.Append(" font-size: 10px")
                End If
                sbSpecHtml.Append("'>" + dtContainerStep.Rows(i)("checkemployeename").ToString + "</td><td style='text-align: left;")
            End If

            'strTmpMemo += dtContainerStep.Rows(i)("NOTES").ToString + ","

            '工序备注栏追加显示 检验员填写的备注信息 add zhunan 2022.4.24
            'If dtContainerStep.Rows(i)("CHECKNOTE").ToString <> " Then" Then
            '    strTmpMemo += dtContainerStep.Rows(i)("CHECKNOTE").ToString + ","
            'End If
            strTmpMemo += strBmemo
            strTmpMemo = strTmpMemo.Trim(",")
            If strTmpMemo.Length > 11 Then
                sbSpecHtml.Append(" font-size: 10px")
            End If
            sbSpecHtml.Append("'>" + strTmpMemo + "</td>")
        End If

        sbSpecHtml.Append("</tr>")
        Return sbSpecHtml.ToString()
    End Function
    
    Private Function GetDataValue(ByVal dtDataPoint As DataTable, ByVal strName As String) As String
        Dim strFilter As String = "dataname='熔炉号子'"
        Dim tmpRows As DataRow() = dtDataPoint.Select(String.Format("dataname='{0}'", strName))
        GetDataValue = ""
        If tmpRows.Length > 0 Then
            If tmpRows(0).IsNull("datavalue") = False Then
                GetDataValue = tmpRows(0)("datavalue").ToString()
            End If
        End If
    End Function
    
    Private Function GetDataValue(ByVal dtDataPoint As DataTable, ByVal strName As String, ByVal IsDefault As Boolean) As String
        Dim strFilter As String = "dataname='熔炉号子'"
        Dim tmpRows As DataRow() = dtDataPoint.Select(String.Format("dataname='{0}'", strName))
        If IsDefault Then
            GetDataValue = "----------"
        Else
            GetDataValue = ""
        End If

        If tmpRows.Length > 0 Then
            If tmpRows(0).IsNull("datavalue") = False Then
                GetDataValue = tmpRows(0)("datavalue").ToString()
            End If
        End If
    End Function
    ''' <summary>
    ''' 保存报工数据
    ''' </summary>
    ''' <param name="context"></param>
    ''' <remarks></remarks>
    Public Sub SaveStepWorkReport(ByVal context As HttpContext)
        Dim result = New uMESProcessResult()
        result.Result = True
        If m_UserInfo.FactoryID Is Nothing Then
            result.Result = False
            result.Message = "缓存过期"
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        '报工批次工序
        Dim dtContainer As DataTable = JsonConvert.DeserializeObject(Of DataTable)(context.Request("paramDt").ToString())
        '检测项是否录入完成 add:Wangjh 20220905 
        result = m_ContainerObj.JudgeContainerCheckPoint(dtContainer)
        If result.Result =False  Then
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        '
        If dtContainer.Rows.Count = 0 Then
            result.Result = False
            result.Message = "没有选择批次"
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        Dim container As DataRow = dtContainer.Rows(0)
        Dim msg As String = ""
        If m_Common.JudeContainerHold(container("ContainerId").ToString(), msg) Then
            result.Result = False
            result.Message = "该批次已暂停，暂停原因：" + msg
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        Dim count = m_ReportBusiness.GetContainerCurrentIsReportType_3(container("ContainerId").ToString(), container("WorkflowstepId").ToString())
        If count > 0 And container("stepchecknum").ToString() = "1" Then
            result.Result = False
            result.Message = "该序存在测试检验"
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        Dim childDt As New DataTable
        If container("ContainerType").ToString() = "3" Then
            childDt = m_Common.GetContainerInfoByPerContianerID(container("ContainerId").ToString())
            childDt = childDt.DefaultView.ToTable(True, "ContainerId")
        End If
        '判断是否报工
        Dim para As New Dictionary(Of String, String)
        para("ContainerId") = container("ContainerId").ToString()
        para("WorkflowstepId") = container("WorkflowstepId").ToString()
        If m_ReportBusiness.IsExistWorkReported(para) Then
            result.Result = False
            result.Message = "该工序已报工"
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        para("ProductName") = container("productName").ToString()
        para("WorkflowStepID") = container("WorkflowstepId").ToString()
        para("ReportQty") = container("Qty").ToString()
        '判断是否有效的周期性检测
        If Not periodicCheck.IsVaildPeriodicForReport(para, msg) Then
            result.Result = False
            result.Message = msg
            context.Response.Write(JsonConvert.SerializeObject(result))
            Exit Sub
        End If
        '填写报工信息
        Dim SQLStringList As New List(Of Object())
        Dim DTO As New ContainerStepWorkReportDTO
        DTO.WorkerID = m_UserInfo.EmployeeID
        DTO.FactoryID = m_UserInfo.FactoryID
        DTO.WorkCenterID = m_UserInfo.WorkCenterID
        DTO.TeamID = m_UserInfo.TeamID
        DTO.FirstContainerNo = "" '首件批次号'
        DTO.KeyProcessBatchNo = "" '关键过程批次号 
        DTO.SelfCheckOutofToleranceQty = 0 '自检超差数
        DTO.SelfCheckLossQty = 0 '自检报废数
        DTO.Note = container("WorkNotes").ToString.Trim() '备注

        Dim strSelectChecker As String = m_Common.GetFatoryAttributes(m_UserInfo.FactoryID, "报工选择检验") '获取工厂报工是否选择检验员信息
        If strSelectChecker = "是" Then
            Dim strRole As String = ""
            strRole = m_Common.GetEnterpriseAttributes("检验员角色")
            If strRole = "" Then
                strRole = "WorkReportTFW"
            End If
            para.Clear()
            para.Add("RoleName", strRole)
            para.Add("FactoryID", m_UserInfo.FactoryID)
            Dim dt As DataTable = m_Common.GetEmployeeRoles(para)
            DTO.CheckerID = IIf(dt.Rows.Count > 0, dt.Rows(0)("CHECKERID").ToString, "")
        Else
            DTO.CheckerID = ""
        End If
        Dim strSelectChecker_ As String = m_Common.GetFatoryAttributes(DTO.FactoryID, "报工自检") '获取工厂报工是否有自检信息
        If strSelectChecker_ <> "是" Then
            DTO.Qty = container("Qty").ToString '不是自检的则报工数就是填写的报工数
            DTO.SeleCheckQualifiedQty = 0
        Else
            DTO.SeleCheckQualifiedQty = container("Qty").ToString
            DTO.Qty = container("Qty").ToString
        End If
        
        DTO.ContainerID = container("ContainerId").ToString
        DTO.WorkflowstepID = container("WorkflowstepId").ToString
        DTO.ResourceID = ""
        DTO.CheckType = 2
        DTO.ContainerStepDispatchID = container("ContainerStepDispatchID").ToString()
        Dim contianerstepworkreportid As String = System.Guid.NewGuid.ToString
        DTO.ContainerStepWorkReportID = contianerstepworkreportid
        SQLStringList.Add(m_ReportBusiness.FillContainerStepWorkReportInfoTrans(DTO, ""))
        para.Clear()
        para.Add("type", "2")
        para.Add("WorkerID", DTO.WorkerID)
        para.Add("ContainerStepDispatchID", container("ContainerStepDispatchID").ToString())
        SQLStringList.Add(m_ReportBusiness.FillReceviceDispatch(para))
        SQLStringList.Add(New Object() {m_ReportBusiness.UpdateStepCheckPointReportId(DTO), Nothing})
        SQLStringList.Add(New Object() {periodicCheck.UpdatePeriodicCheckSurplusQty(container("productName").ToString(), DTO.WorkflowstepID, DTO.Qty)}) '更新周期性检测的剩余数量
        '插入明细表
        For Each row As DataRow In childDt.Rows
            Dim entity As New ContainerStepWorkReportChildDTO
            entity.ChildContainerID = row("ContainerId")
            entity.SelfCheckLossQty = "0"
            entity.ContainerStepWorkReportID = contianerstepworkreportid
            SQLStringList.Add(m_ReportBusiness.FillContainerStepWorkReportChildInfo(entity))
        Next
        
        '将批次总表检验标识更新，如果是检验序则在服务中填写报工记录后将标识更新add by tianFW 2019年1月3日
        SQLStringList.Add(m_ReportBusiness.FillUpdateStepCheckNum(container("ContainerId").ToString, 2))

        If SQLStringList.Count > 0 Then
            If Not DatabaseHelper.ExecuteSqlTran(SQLStringList) Then
                result.Result = False
                result.Message = "报工异常"
                context.Response.Write(JsonConvert.SerializeObject(result))
                Exit Sub
            End If
        End If
        Dim ContainerStepDispatchInfoIDs As String = " csw.containerstepdispatchid='" & container("ContainerStepDispatchID").ToString() & "'"
        m_ReportBusiness.UpdateWorkReportNum(container("ContainerId").ToString) '更新批次当前序的报工数
        m_DispatchObj.UpdateDispatchIsCompleteStatus(ContainerStepDispatchInfoIDs) '更新派工记录是否完工状态
        result.Message = "报工成功！"
        context.Response.Write(JsonConvert.SerializeObject(result))
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class